package com.xuxin.baby.popup;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.AttachPopupView;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.ChildPopupAdapter;
import com.xuxin.baby.bean.UserChildBean;
import com.xuxin.baby.utils.RecycleUtils;

import io.reactivex.rxjava3.annotations.NonNull;

/**
 * Created by xu_xin
 * Created on 2020/11/11 0011
 */
public class AttachChildPopup extends AttachPopupView {
    private final ChildPopupAdapter myAdapter;

    private OnChildClickListener onChildClickListener;

    public interface OnChildClickListener {
        void click(UserChildBean.ResultBean.ItemsBean itemsBean);
    }

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    public AttachChildPopup(@NonNull Context context) {
        super(context);
        myAdapter = new ChildPopupAdapter();
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@androidx.annotation.NonNull BaseQuickAdapter<?, ?> adapter, @androidx.annotation.NonNull View view, int position) {
                if (null != onChildClickListener) {
                    try {
                        onChildClickListener.click(myAdapter.getData().get(position));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void refresh(UserChildBean userChildBean) {
        myAdapter.setList(userChildBean.getResult().getItems());
    }

    public int getData() {
        if (null != myAdapter) {
            return myAdapter.getData().size();
        } else {
            return 0;
        }
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.attach_child_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        RecyclerView attach_child_recycle = findViewById(R.id.attach_child_recycle);
        RecycleUtils.init(attach_child_recycle);
        attach_child_recycle.setAdapter(myAdapter);
    }
}